package com.lx.timer.springtask.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * 线程池配置
 * 需要自定义一个线程池配置后才能使springtask为异步支持
 * 否则加Async依然是串行的
 * @author 段誉
 * @create 2019-04-02 14:26
 */
@Configuration
//开启异步事件的支持
@EnableAsync
public class AsyncConfig {
  @Value("${async.core-pool-size}")
  private int corePoolSize;
  @Value("${async.max-pool-size}")
  private int maxPoolSize;
  @Value("${async.queue-capacity}")
  private int quequeCapacity;

  @Bean
  public Executor taskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(corePoolSize);
    executor.setMaxPoolSize(maxPoolSize);
    executor.setQueueCapacity(quequeCapacity);
    executor.initialize();
    return executor;
  }
}
